package com.comment.config.dao.typeHandler;

import cn.hutool.json.JSONUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ListIntegerHandler extends BaseTypeHandler<List<Integer>> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<Integer> parameter, JdbcType jdbcType) throws SQLException {
        String serialized = JSONUtil.toJsonStr(parameter);
        ps.setString(i, serialized);
    }

    @Override
    public List<Integer> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String serialized = rs.getString(columnName);

        return new ArrayList<>(JSONUtil.toList(serialized, Integer.class));
    }

    @Override
    public List<Integer> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String serialized = rs.getString(columnIndex);
        return new ArrayList<>(JSONUtil.toList(serialized, Integer.class));
    }

    @Override
    public List<Integer> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String serialized = cs.getString(columnIndex);
        return new ArrayList<>(JSONUtil.toList(serialized, Integer.class));
    }

}
